//
//  ZHFTripCell.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFTripCell.h"

#import "ZHFTrip.h"
#import "UIImageView+WebCache.h"
#import "ZHFUser.h"
#import "ZHFNotes.h"
#import "WSTools.h"
#import "ZHFImageView.h"

@interface ZHFTripCell ()

@property (nonatomic, retain) UIImageView *headimage;

@property (nonatomic, retain) UILabel *nameLabel;

@property (nonatomic, retain) UILabel *contentLabel;

@property (nonatomic, retain) UIScrollView *scrollView;

@end

@implementation ZHFTripCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createSubView];
    }
    return self;
}

- (void)createSubView {
    
    self.headimage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 30, 30)];
    self.headimage.layer.cornerRadius = self.headimage.frame.size.width / 2.0;
    self.headimage.layer.masksToBounds = YES;
    self.headimage.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.headimage];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 15, SCREEN_WIDTH - 60, 30)];
    
    self.nameLabel.textColor = [UIColor colorWithRed:0.26 green:0.24 blue:0.19 alpha:1];
    self.nameLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:self.nameLabel];
    
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 60, SCREEN_WIDTH - 30, 30)];
    
    self.contentLabel.textColor = [UIColor colorWithRed:0.26 green:0.24 blue:0.19 alpha:1];
    self.contentLabel.font = [UIFont systemFontOfSize:16];
    self.contentLabel.numberOfLines = 0;
    [self.contentView addSubview:self.contentLabel];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(1, 100, SCREEN_WIDTH - 2, 190)];
    
    self.scrollView.backgroundColor = self.contentView.backgroundColor;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.contentView addSubview:self.scrollView];
}

- (void)setTrip:(ZHFTrip *)trip{
    
    [self.headimage sd_setImageWithURL:[NSURL URLWithString:trip.user.image] placeholderImage:[UIImage imageNamed:@"avatar_placeholder_26"]];
    self.nameLabel.text = trip.user.name;
    self.contentLabel.text = trip.descriptions;
    
    CGRect frame = self.contentLabel.frame;
    CGFloat height = [WSTools heightForContent:self.trip.descriptions size:16];
    frame.size.height = height;
    self.contentLabel.frame = frame;
    
    frame = self.scrollView.frame;
    frame.origin.y = 70 + height;
    self.scrollView.frame = frame;
    
    CGFloat w = 5;
    for (UIView *view in self.scrollView.subviews) {
        [view removeFromSuperview];
    }
    for (ZHFNotes *notes in trip.tripArray) {
        CGFloat f = [notes.width floatValue] / [notes.height floatValue];
        
        ZHFImageView *occupyImage = [[ZHFImageView alloc] initWithFrame:CGRectMake(w, 0, 190 * f, 190)];
        [self.scrollView addSubview:occupyImage];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(w, 0, 190 * f, 190)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:notes.photo_url]];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.masksToBounds = YES;
        [self.scrollView addSubview:imageView];
        
        w = f * 190 + w + 5;
    }
    
    self.scrollView.contentSize = CGSizeMake(w, 190);
    if (w == 5) {
        [self.scrollView removeFromSuperview];
    }
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
